<template>
  <div class="story-list-container">
    <div class="story-header">
      <h1 class="page-title">我们来听你的故事</h1>
      <div class="page-divider"></div>
    </div>

    <div class="story-grid">
      <a-card
        v-for="story in stories"
        :key="story.id"
        class="story-card"
        :loading="loading"
        @click="viewStoryDetail(story.id)"
      >
        <div class="card-number">{{ String(story.id).padStart(2, "0") }}</div>
        <div class="card-image">
          <img :src="story.cardImage" :alt="story.title" />
        </div>
        <div class="card-content">
          <h3 class="card-title">{{ story.title }}</h3>
          <p class="card-subtitle">{{ story.subtitle }}</p>
          <div class="card-actions">
            <a-button class="read-btn">阅读</a-button>
          </div>
        </div>
      </a-card>
    </div>

    <!-- 分页 -->
    <div class="pagination-container">
      <a-pagination
        v-model:current="current"
        :total="total"
        :pageSize="pageSize"
        @change="handlePageChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useRouter } from "vue-router";
import { message } from "ant-design-vue";
import { listStoriesByPageUsingGet } from "@/api/cultureStoryController";

const router = useRouter();
const loading = ref(true);
const stories = ref<any[]>([]);
const current = ref(1);
const pageSize = ref(9);
const total = ref(0);

const fetchStories = async () => {
  loading.value = true;
  try {
    const res = await listStoriesByPageUsingGet({
      current: current.value,
      size: pageSize.value,
    });

    if (res.data.code === 0 && res.data.data) {
      stories.value = res.data.data.records || [];
      total.value = Number(res.data.data.total) || 0;
    } else {
      message.error(res.data.message || "获取故事列表失败");
    }
  } catch (error) {
    console.error("获取故事列表失败:", error);
    message.error("获取故事列表失败");
  } finally {
    loading.value = false;
  }
};

const handlePageChange = (page: number) => {
  current.value = page;
  fetchStories();
};

const viewStoryDetail = (id: string) => {
  router.push(`/culture/story/${id}`);
};

onMounted(() => {
  fetchStories();
});
</script>

<style scoped>
.story-list-container {
  width: 100%;
  min-height: 100vh;
  padding: 6vh 0;
  background-color: #f9f5f0;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.story-header {
  text-align: center;
  margin-bottom: 3rem;
  width: 100%;
}

.page-title {
  font-size: 2rem;
  font-weight: bold;
  color: #62432d;
  margin-bottom: 1rem;
}

.page-divider {
  width: 100px;
  height: 2px;
  background-color: #62432d;
  margin: 0 auto;
}

.story-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 2rem;
  max-width: 1200px;
  width: 95%;
  margin: 0 auto;
}

.story-card {
  position: relative;
  border-radius: 12px;
  overflow: hidden;
  transition: all 0.3s ease;
  cursor: pointer;
  background-color: #fffcf5;
  border: 1px solid #e9e1d6;
}

.story-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
}

.card-number {
  position: absolute;
  top: 1rem;
  left: 1rem;
  background-color: #e74c3c;
  color: white;
  font-weight: bold;
  padding: 0.3rem 0.6rem;
  border-radius: 3rem;
  font-size: 0.9rem;
  z-index: 2;
}

.card-image {
  height: 200px;
  overflow: hidden;
}

.card-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s ease;
}

.story-card:hover .card-image img {
  transform: scale(1.05);
}

.card-content {
  padding: 1.5rem;
}

.card-title {
  font-size: 1.2rem;
  color: #333;
  margin-bottom: 0.5rem;
  font-weight: bold;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-subtitle {
  font-size: 0.9rem;
  color: #666;
  margin-bottom: 1.5rem;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.5;
}

.card-actions {
  display: flex;
  justify-content: flex-end;
}

.read-btn {
  background-color: #62432d;
  color: white;
  border: none;
  border-radius: 4px;
}

.read-btn:hover {
  background-color: #7d5a3e;
  color: white;
}

.pagination-container {
  margin-top: 3rem;
}

/* 响应式布局 */
@media (max-width: 1200px) {
  .story-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 768px) {
  .story-grid {
    grid-template-columns: 1fr;
  }

  .page-title {
    font-size: 1.8rem;
  }
}
</style>
